package com.yl.ydk.ui.activity

import android.content.Context
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.yl.ydk.R
import com.yl.ydk.base.BaseActivity
import com.yl.ydk.presenter.contract.DiseaseSearchActivityContract
import com.yl.ydk.presenter.impl.DiseaseSearchActivityImpl
import com.blankj.utilcode.utils.StringUtils
import com.blankj.utilcode.utils.ToastUtils
import kotlinx.android.synthetic.main.activity_disease_search.*
import kotlinx.android.synthetic.main.roob_bar.*

class DiseaseSearchActivity : BaseActivity(), View.OnClickListener,
    DiseaseSearchActivityContract.View {

    private val mPresenter = DiseaseSearchActivityImpl()

    override fun getLayoutId(): Int = R.layout.activity_disease_search

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun detachView() {
        mPresenter.detachView()
    }

    override fun initView() {

        tvTitle.text = "搜索"
        rlBack.setOnClickListener(this)
        rlSearchBack.setOnClickListener(this)
        rlSearch1.setOnClickListener(this)
        rlSearch2.setOnClickListener(this)
        rlSearch3.setOnClickListener(this)

        etSearch.setOnEditorActionListener(object : TextView.OnEditorActionListener {
            override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
                if (StringUtils.isEmpty(etSearch.text.toString().trim())) {
                    ToastUtils.showLongToastSafe("请输入搜索内容")
                    return true
                } else {
                    hintKbTwo()
                    mPresenter.getGetDiseaseList(
                        this@DiseaseSearchActivity, "name",
                        etSearch.text.toString().trim()
                    )
                    return true
                }
            }
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rlBack -> {
                finish()
            }
            R.id.rlSearch1 -> {
                mPresenter.getGetDiseaseList(
                    this@DiseaseSearchActivity, "name",
                    tvSearch1.text.toString()
                )
            }
            R.id.rlSearch2 -> {
                mPresenter.getGetDiseaseList(
                    this@DiseaseSearchActivity, "name",
                    tvSearch2.text.toString()
                )
            }
            R.id.rlSearch3 -> {
                mPresenter.getGetDiseaseList(
                    this@DiseaseSearchActivity, "name",
                    tvSearch3.text.toString()
                )
            }
            R.id.rlSearchBack -> {
                if (StringUtils.isEmpty(etSearch.text.toString().trim())) {
                    ToastUtils.showLongToastSafe("请输入搜索内容")
                    return
                }
                hintKbTwo()
                mPresenter.getGetDiseaseList(
                    this@DiseaseSearchActivity, "name",
                    etSearch.text.toString().trim()
                )
            }
        }
    }

    /**
     */
    private fun hintKbTwo() {
        val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        if (imm.isActive && this.currentFocus != null) {
            if (this.currentFocus?.windowToken != null) {
                imm.hideSoftInputFromWindow(
                    this.currentFocus?.windowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS
                );
            }
        }
    }

    override fun getllNormal(): LinearLayout = llNormal

    override fun getRvSearch(): RecyclerView = rvSearch

    override fun getActivity(): DiseaseSearchActivity = this

    override fun showError(message: String?) {
        ToastUtils.showLongToastSafe(message)
    }

}